/*
 * $Id: OfferingCampusAction.java 3 2011-12-26 07:48:43Z forwardly@hotmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.web.programme;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.as.dao.programme.BaseProgrammeDao;
import com.lightsaber.trade.as.dao.programme.StreamDao;
import com.lightsaber.trade.as.entity.programme.BaseProgramme;
import com.lightsaber.trade.as.entity.programme.OfferingCampus;
import com.lightsaber.trade.as.entity.programme.ProgrammeStatus;
import com.lightsaber.trade.as.entity.programme.Stream;
import com.lightsaber.trade.as.service.param.ParamObject;
import com.lightsaber.trade.as.service.param.ParamService;
import com.lightsaber.trade.as.service.programme.OfferingCampusManager;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/programme")
public class OfferingCampusAction extends BaseAction {

    private static final long serialVersionUID = -4872942104147439272L;
    // ****************** Autowired *****************START
    @Autowired
    private transient OfferingCampusManager offeringCampusManager;

    @Autowired
    private transient BaseProgrammeDao baseProgrammeDao;
    
    @Autowired
    private transient ParamService paramService;
    
    @Autowired
    private StreamDao streamDao;
    // ****************** Autowired *****************END

    // ****************** Page parameters *****************START
    private String programmeId;

    private List<OfferingCampus> results;

    private List<CampusDomain> pageDomain;

    private String[] campusIds;

    private String campusCode;
    
    private boolean canEdit;

    // ****************** Page parameters *****************END

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        BaseProgramme programme = baseProgrammeDao.get(programmeId);
        if(programme.getProgrammeKind().equals("Stream")){
            ProgrammeStatus status = streamDao.get(programmeId).getProgramme().getStatus();
            canEdit = null==status?true:status.isCanEdit();
         }else{
             canEdit = null==programme.getStatus()?true:programme.getStatus().isCanEdit();
         }
        
        results = offeringCampusManager.findByProgramme(programme);
      
        // Generate project to display on the page
        pageDomain = new ArrayList<CampusDomain>();
        for (OfferingCampus offeringCampus : results) {
            CampusDomain domain = new CampusDomain();
            domain.setOfferingCampus(offeringCampus);

            domain.setOfferingDepartments(paramService.getDepartmentByCampus(offeringCampus
                    .getCampus()));

            domain.setOperatingDepartments(paramService.getDepartmentByCampus(offeringCampus
                    .getOperatingCampus()));

            pageDomain.add(domain);
        }

        

        return SUCCESS;
    }

    public String add() throws Exception {
        if (results == null) {
            results = new LinkedList<OfferingCampus>();
        }

        BaseProgramme programme = baseProgrammeDao.get(programmeId);
        OfferingCampus offeringCampus = new OfferingCampus();
        offeringCampus.setProgramme(programme);
        offeringCampus.setCampus("0");
        offeringCampus.setOfferingUnit("0");
        offeringCampus.setOperatingCampus("0");// TODO default value
        offeringCampus.setOperatingUnit("0");// TODO default value
        offeringCampusManager.save(offeringCampus);

        addActionMessage(getText(PROCESSED_SUCCESS));

        return list();
    }

    public String save() throws Exception {
        if (results != null && results.size() > 0) {

            offeringCampusManager.batchSave(results);
        }
        addActionMessage(getText(PROCESSED_SUCCESS));

        return list();
    }

    public String delete() throws Exception {

        // 删除选中的： 当页面选中了, id赋空值
        if (campusIds != null && campusIds.length > 0) {
            for (String id : campusIds) {
                offeringCampusManager.delete(id);
            }
        }
        addActionMessage(getText(PROCESSED_SUCCESS));

        return list();
    }

    public void ajaxGetDepartByCumpus() {
        List<ParamObject> departmetns = paramService.getDepartmentByCampus(campusCode);
        Struts2Utils.renderJson(departmetns);
    }

    public String getProgrammeId() {
        return programmeId;
    }

    public void setProgrammeId(final String programmeId) {
        this.programmeId = programmeId;
    }

    public List<OfferingCampus> getResults() {
        return results;
    }

    public void setResults(final List<OfferingCampus> results) {
        this.results = results;
    }

    public String[] getCampusIds() {
        return campusIds;
    }

    public void setCampusIds(final String[] campusIds) {
        this.campusIds = campusIds;
    }

    public ParamService getParamService() {
        return paramService;
    }

    public void setParamService(final ParamService paramService) {
        this.paramService = paramService;
    }

    public String getCampusCode() {
        return campusCode;
    }

    public void setCampusCode(final String campusCode) {
        this.campusCode = campusCode;
    }

    public List<CampusDomain> getPageDomain() {
        return pageDomain;
    }

    public void setPageDomain(final List<CampusDomain> pageDomain) {
        this.pageDomain = pageDomain;
    }

    public boolean isCanEdit() {
        return canEdit;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }
}
